﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MIMSDomain.Abstract;
using MIMSDomain.Entities;
using System.Linq.Expressions;

namespace MIMSDomain.Concrete
{
    public class EFPatientRepository : IRepository<Patient>, IDisposable
    {
        EFDbContext dbContext = new EFDbContext();

        public virtual IQueryable<Patient> All
        {
            get 
            {
                return dbContext.Patients;
            }
        }

        public virtual bool Save(Patient _T)
        {
            try
            {
                if (_T.Id == 0)
                {
                    dbContext.Patients.Add(_T);
                }
                //else
                //{
                //    return false;
                //}

                dbContext.SaveChanges();
                return true;
            }
            catch (Exception Ex)
            {
                //Log Exception
                throw new Exception(Ex.Message);
            }
        }

        public virtual Patient GetById(int Id)
        {
            try
            {
                return dbContext.Patients.Find(Id);
            }
            catch (Exception Ex)
            {
                //Log Exception
                throw new Exception(Ex.Message);
            }
        }
        public virtual bool Delete(Patient _T)
        {
            try
            {
                dbContext.Patients.Remove(_T);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception Ex)
            {
                //Log Exception
                throw new Exception(Ex.Message);
            }
        }

        public IQueryable<Patient> Search(Expression<Func<Patient, bool>> predicate)
        {
            return dbContext.Patients.Where(predicate);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                dbContext.Dispose();
            }
        }
    }
}
